#include "Main.h"

sf::RenderWindow App;
sf::String Text("Hello",sf::Font::GetDefaultFont(),14);
float arrViewAngles[3] = {0.0f,0.0f,0.0f};
float arrCamPosition[3] = {-1.0f,0.0f,-3.0f};
float f_mouse_sensitivity = 0.5f;
float f_walk_velocity = 0.01f;



bool populateTriangleListDone = false;

std::list<Triangle> listTriangle;

int main(int argc, char** argv){
	sf::WindowSettings Settings;
	Settings.DepthBits = 24;
	Settings.StencilBits = 8;
	Settings.AntialiasingLevel = 0;

	App.Create(sf::VideoMode(640,480,32), "Main Window",sf::Style::Close,Settings);

	//Initialize Input Handler
	
	GameInput gameInput;

	//Load Map
	sf::Image Image;
	Image.LoadFromFile("collisionmap.bmp");
	
	//Initialize OpenGL Begin

	glClearDepth(1.0f);
	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_TRUE);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f,(GLfloat)640/(GLfloat)480,0.1f,500.0f);
	glViewport(0,0,640,480);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	App.PreserveOpenGLStates(true);
	//Initialize OpenGL End

	
	App.ShowMouseCursor(false);
	
	while(App.IsOpened()){
		
		sf::Event Event;
		while (App.GetEvent(Event)){
			if(Event.Type == sf::Event::Closed)
				App.Close();

		}
		gameInput.GetInputs();

		App.SetCursorPosition(320,240);

		//OpenGL Begin

		App.SetActive();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
		
		//Bounding Circle
		/*glLoadIdentity();
		glRotatef(arrViewAngles[PITCH],1.0f,0.0f,0.0f);
		glTranslatef(0.0f,arrCamPosition[Y]+0.0f,0.0f);
		GLUquadricObj *quadric = gluNewQuadric();
		gluSphere(quadric,1.0,20,16);
		gluDeleteQuadric(quadric);
		*/

		glLoadIdentity();
		glRotatef(arrViewAngles[PITCH],1.0f,0.0f,0.0f);
		glRotatef(arrViewAngles[YAW],0.0f,1.0f,0.0f);
		glTranslatef(arrCamPosition[X],arrCamPosition[Y],arrCamPosition[Z]);
		float temptileheight=0.0f;
		for(int i=0;i<Image.GetHeight();i++){
			for(int j=0;j<Image.GetWidth();j++){
				if(Image.GetPixel(j,i) == sf::Color::Black){
					drawBlock(j*2.0f,i*2.0f);
					
					//drawTileHeightBlock(j*2.0f,temptileheight,i*2.0f);
					temptileheight+=0.25f;
				}
				
			}
		}

		//run once
		
		//draw floor
		glBegin(GL_QUADS);
			glColor3f(1.0f,0.5f,0.0f);
			glVertex3f(10.0f,	-1.0f,	-10.0f);			// Top Right Of The Quad (Top)
			glVertex3f(-10.0f,	-1.0f,	-10.0f);			// Top Left Of The Quad (Top)
			glVertex3f(-10.0f,	-1.0f,	10.0f);			// Bottom Left Of The Quad (Top)
			glVertex3f(10.0f,	-1.0f,	10.0f);			// Bottom Right Of The Quad (Top)
		glEnd();
		if(!populateTriangleListDone){
			Triangle triA;
			triA.v1.x = 10.0f;
			triA.v1.y = -1.0f;
			triA.v1.z = -10.0f;
			triA.v2.x = -10.0f;
			triA.v2.y = -1.0f;
			triA.v2.z = -10.0f;
			triA.v3.x = -10.0f;
			triA.v3.y = -1.0f;
			triA.v3.z = 10.0f;
			listTriangle.push_back(triA);
			triA.v1.x = 10.0f;
			triA.v1.y = -1.0f;
			triA.v1.z = -10.0f;
			triA.v2.x = -10.0f;
			triA.v2.y = -1.0f;
			triA.v2.z = 10.0f;
			triA.v3.x = 10.0f;
			triA.v3.y = -1.0f;
			triA.v3.z = 10.0f;
			listTriangle.push_back(triA);
		}
		populateTriangleListDone = true;
		//OpenGL End
		App.Draw(Text);
		App.Display();
	}

	return EXIT_SUCCESS;

}

void drawTileHeightBlock(float x, float y, float z){
	glBegin(GL_QUADS);
		glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Blue
		glVertex3f(x,		y-0.75f,	z-2.0f);			// Top Right Of The Quad (Top)
		glVertex3f(x-2.0f,	y-0.75f,	z-2.0f);			// Top Left Of The Quad (Top)
		glVertex3f(x-2.0f,	y-0.75f,	z);			// Bottom Left Of The Quad (Top)
		glVertex3f(x,		y-0.75f,	z);			// Bottom Right Of The Quad (Top)

		glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange
		glVertex3f(x,		y-1.0f,	z);			// Top Right Of The Quad (Bottom)
		glVertex3f(x-2.0f,	y-1.0f,	z);			// Top Left Of The Quad (Bottom)
		glVertex3f(x-2.0f,	y-1.0f,	z-2.0f);			// Bottom Left Of The Quad (Bottom)
		glVertex3f(x,		y-1.0f,	z-2.0f);			// Bottom Right Of The Quad (Bottom)

		glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
		glVertex3f(x,		y-0.75f,	z);			// Top Right Of The Quad (Front)
		glVertex3f(x-2.0f,	y-0.75f,	z);			// Top Left Of The Quad (Front)
		glVertex3f(x-2.0f,	y-1.0f,	z);			// Bottom Left Of The Quad (Front)
		glVertex3f(x,		y-1.0f,	z);			// Bottom Right Of The Quad (Front)
		
		glColor3f(1.0f,1.0f,0.0f);			// Set The Color To zellow
		glVertex3f(x,		y-1.0f,	z-2.0f);			// Bottom Left Of The Quad (Back)
		glVertex3f(x-2.0f,	y-1.0f,	z-2.0f);			// Bottom Right Of The Quad (Back)
		glVertex3f(x-2.0f,	y-0.75f,	z-2.0f);			// Top Right Of The Quad (Back)
		glVertex3f(x,		y-0.75f,	z-2.0f);			// Top Left Of The Quad (Back)

		glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
		glVertex3f(x-2.0f,	y-0.75f,	z);			// Top Right Of The Quad (Left)
		glVertex3f(x-2.0f,	y-0.75f,	z-2.0f);			// Top Left Of The Quad (Left)
		glVertex3f(x-2.0f,	y-1.0f,	z-2.0f);			// Bottom Left Of The Quad (Left)
		glVertex3f(x-2.0f,	y-1.0f,	z);			// Bottom Right Of The Quad (Left)
		
		glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet
		glVertex3f(x,		y-0.75f,	z-2.0f);			// Top Right Of The Quad (Right)
		glVertex3f(x,		y-0.75f,	z);			// Top Left Of The Quad (Right)
		glVertex3f(x,		y-1.0f,	z);			// Bottom Left Of The Quad (Right)
		glVertex3f(x,		y-1.0f,	z-2.0f);			// Bottom Right Of The Quad (Right)
	glEnd();
	if(!populateTriangleListDone){
		//Front
		Triangle triA;
		triA.v1.x = x;
		triA.v1.y = y-0.75f;
		triA.v1.z = z;
		triA.v2.x = x-2.0f;
		triA.v2.y = y-0.75f;
		triA.v2.z = z;
		triA.v3.x = x-2.0f;
		triA.v3.y = y-1.0f;
		triA.v3.z = z;
		listTriangle.push_back(triA);
		triA.v1.x = x;
		triA.v1.y = y-0.75f;
		triA.v1.z = z;
		triA.v2.x = x-2.0f;
		triA.v2.y = y-1.0f;
		triA.v2.z = z;
		triA.v3.x = x;
		triA.v3.y = y-1.0f;
		triA.v3.z = z;
		listTriangle.push_back(triA);
		//Top
		triA.v1.x = x;
		triA.v1.y = y-0.75f;
		triA.v1.z = z-2.0f;
		triA.v2.x = x-2.0f;
		triA.v2.y = y-0.75f;
		triA.v2.z = z-2.0f;
		triA.v3.x = x-2.0f;
		triA.v3.y = y-0.75f;
		triA.v3.z = z;
		listTriangle.push_back(triA);
		triA.v1.x = x;
		triA.v1.y = y-0.75f;
		triA.v1.z = z-2.0f;
		triA.v2.x = x-2.0f;
		triA.v2.y = y-0.75f;
		triA.v2.z = z;
		triA.v3.x = x;
		triA.v3.y = y-0.75f;
		triA.v3.z = z;
		listTriangle.push_back(triA);
		//Back
		triA.v1.x = x-2.0f;
		triA.v1.y = y-0.75f;
		triA.v1.z = z-2.0f;
		triA.v2.x = x;
		triA.v2.y = y-0.75f;
		triA.v2.z = z-2.0f;
		triA.v3.x = x;
		triA.v3.y = y-1.0f;
		triA.v3.z = z-2.0f;
		listTriangle.push_back(triA);
		triA.v1.x = x-2.0f;
		triA.v1.y = y-0.75f;
		triA.v1.z = z-2.0;
		triA.v2.x = x;
		triA.v2.y = y-1.0f;
		triA.v2.z = z-2.0f;
		triA.v3.x = x-2.0f;
		triA.v3.y = y-1.0f;
		triA.v3.z = z-2.0f;
		listTriangle.push_back(triA);
		//Left
		triA.v1.x = x-2.0f;
		triA.v1.y = y-0.75f;
		triA.v1.z = z;
		triA.v2.x = x-2.0f;
		triA.v2.y = y-0.75f;
		triA.v2.z = z-2.0;
		triA.v3.x = x-2.0f;
		triA.v3.y = y-1.0f;
		triA.v3.z = z-2.0;
		listTriangle.push_back(triA);
		triA.v1.x = x-2.0f;
		triA.v1.y = y-0.75f;
		triA.v1.z = z;
		triA.v2.x = x-2.0f;
		triA.v2.y = y-1.0f;
		triA.v2.z = z-2.0;
		triA.v3.x = x-2.0f;
		triA.v3.y = y-1.0f;
		triA.v3.z = z;
		listTriangle.push_back(triA);
		//Right
		triA.v1.x = x;
		triA.v1.y = y-0.75f;
		triA.v1.z = z-2.0f;
		triA.v2.x = x;
		triA.v2.y = y-0.75f;
		triA.v2.z = z;
		triA.v3.x = x;
		triA.v3.y = y-1.0f;
		triA.v3.z = z;
		listTriangle.push_back(triA);
		triA.v1.x = x;
		triA.v1.y = y-0.75f;
		triA.v1.z = z-2.0f;
		triA.v2.x = x;
		triA.v2.y = y-1.0f;
		triA.v2.z = z;
		triA.v3.x = x;
		triA.v3.y = y-1.0f;
		triA.v3.z = z-2.0f;
		listTriangle.push_back(triA);
		
	}
}

void drawBlock(float x, float y){
	glBegin(GL_QUADS);
		glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green
		glVertex3f(x, 1.0f,y-2.0f);			// Top Right Of The Quad (Top)
		glVertex3f(x-2.0f, 1.0f,y-2.0f);			// Top Left Of The Quad (Top)
		glVertex3f(x-2.0f, 1.0f,y);			// Bottom Left Of The Quad (Top)
		glVertex3f(x, 1.0f,y);			// Bottom Right Of The Quad (Top)

		glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange
		glVertex3f(x,-1.0f,y);			// Top Right Of The Quad (Bottom)
		glVertex3f(x-2.0f,-1.0f,y);			// Top Left Of The Quad (Bottom)
		glVertex3f(x-2.0f,-1.0f,y-2.0f);			// Bottom Left Of The Quad (Bottom)
		glVertex3f(x,-1.0f,y-2.0f);			// Bottom Right Of The Quad (Bottom)

		glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
		glVertex3f(x, 1.0f,y);			// Top Right Of The Quad (Front)
		glVertex3f(x-2.0f, 1.0f,y);			// Top Left Of The Quad (Front)
		glVertex3f(x-2.0f,-1.0f,y);			// Bottom Left Of The Quad (Front)
		glVertex3f(x,-1.0f,y);			// Bottom Right Of The Quad (Front)

		glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
		glVertex3f(x-2.0f,1.0f,y-2.0f);			// Bottom Left Of The Quad (Back)
		glVertex3f(x,1.0f,y-2.0f);			// Bottom Right Of The Quad (Back)
		glVertex3f(x,-1.0f,y-2.0f);			// Top Right Of The Quad (Back)
		glVertex3f(x-2.0f,-1.0f,y-2.0f);			// Top Left Of The Quad (Back)

		glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
		glVertex3f(x-2.0f,1.0f,y);			// Top Right Of The Quad (Left)
		glVertex3f(x-2.0f,1.0f,y-2.0f);			// Top Left Of The Quad (Left)
		glVertex3f(x-2.0f,-1.0f,y-2.0f);			// Bottom Left Of The Quad (Left)
		glVertex3f(x-2.0f,-1.0f,y);			// Bottom Right Of The Quad (Left)

		glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet
		glVertex3f(x, 1.0f,y-2.0f);			// Top Right Of The Quad (Right)
		glVertex3f(x, 1.0f,y);			// Top Left Of The Quad (Right)
		glVertex3f(x,-1.0f,y);			// Bottom Left Of The Quad (Right)
		glVertex3f(x,-1.0f,y-2.0f);			// Bottom Right Of The Quad (Right)

	glEnd();
	if(!populateTriangleListDone){
		Triangle triA;
		//Front

		triA.v1.x = x;
		triA.v1.y = 1.0f;
		triA.v1.z = y;
		triA.v2.x = x-2.0f;
		triA.v2.y = 1.0f;
		triA.v2.z = y;
		triA.v3.x = x-2.0f;
		triA.v3.y = -1.0f;
		triA.v3.z = y;
		listTriangle.push_back(triA);
		triA.v1.x = x;
		triA.v1.y = 1.0f;
		triA.v1.z = y;
		triA.v2.x = x-2.0f;
		triA.v2.y = -1.0f;
		triA.v2.z = y;
		triA.v3.x = x;
		triA.v3.y = -1.0f;
		triA.v3.z = y;
		listTriangle.push_back(triA);
		//Back
		triA.v1.x = x-2.0f;
		triA.v1.y = 1.0f;
		triA.v1.z = y-2.0;
		triA.v2.x = x;
		triA.v2.y = 1.0f;
		triA.v2.z = y-2.0f;
		triA.v3.x = x;
		triA.v3.y = -1.0f;
		triA.v3.z = y-2.0f;
		listTriangle.push_back(triA);
		triA.v1.x = x-2.0f;
		triA.v1.y = 1.0f;
		triA.v1.z = y-2.0f;
		triA.v2.x = x;
		triA.v2.y = -1.0f;
		triA.v2.z = y-2.0f;
		triA.v3.x = x-2.0f;
		triA.v3.y = -1.0f;
		triA.v3.z = y-2.0f;
		listTriangle.push_back(triA);
		
		//Left
		triA.v1.x = x-2.0f;
		triA.v1.y = 1.0f;
		triA.v1.z = y;
		triA.v2.x = x-2.0f;
		triA.v2.y = 1.0f;
		triA.v2.z = y-2.0;
		triA.v3.x = x-2.0f;
		triA.v3.y = -1.0f;
		triA.v3.z = y-2.0;
		listTriangle.push_back(triA);
		triA.v1.x = x-2.0f;
		triA.v1.y = 1.0f;
		triA.v1.z = y;
		triA.v2.x = x-2.0f;
		triA.v2.y = -1.0f;
		triA.v2.z = y-2.0f;
		triA.v3.x = x-2.0f;
		triA.v3.y = -1.0f;
		triA.v3.z = y;
		listTriangle.push_back(triA);
		
		//Right
		triA.v1.x = x;
		triA.v1.y = 1.0f;
		triA.v1.z = y-2.0f;
		triA.v2.x = x;
		triA.v2.y = 1.0f;
		triA.v2.z = y;
		triA.v3.x = x;
		triA.v3.y = -1.0f;
		triA.v3.z = y;
		listTriangle.push_back(triA);
		triA.v1.x = x;
		triA.v1.y = 1.0f;
		triA.v1.z = y-2.0f;
		triA.v2.x = x;
		triA.v2.y = -1.0f;
		triA.v2.z = y;
		triA.v3.x = x;
		triA.v3.y = -1.0f;
		triA.v3.z = y-2.0f;
		listTriangle.push_back(triA);
		
	}
}